<script setup lang="ts">
import  {ref} from 'vue'
import{mobileRules,passwordRules,codeRules} from'@/utils/rules.ts'
import { showSuccessToast, showToast } from 'vant';
// import 
// 导入接口
import {loginByPasswordAPI,sendCodeAPI,LoginByCodeAPI} from '@/api/user'
import {useUserStore} from '@/stores/index'
import {useRouter,useRoute} from 'vue-router'

const router=useRouter()//此router是操作路由
const route=useRoute()//此route是操作url的

const store=useUserStore()
const mobile=ref<string>('13230000080')
const password=ref<string>("abc12345")
const agree =ref(false)
const isPassword=ref(false)
// const code=ref(true)
const code=ref<string>('')

// 输入密码控制眼睛的开关
const show = ref(false)

// 登录
const login=async ()=>{


  if(!agree.value) {
    showToast("请勾选同意")
    return
  }  

  if(isPassword.value){
    const res=await loginByPasswordAPI(mobile.value,password.value)
    store.setUser(res.data)
    showSuccessToast("登录成功")
  }else{
    const  res=await LoginByCodeAPI(mobile.value,code.value)
    store.setUser(res.data)
    console.log(res);
    
  }


  // 登录部分
  // 调用等接口进行登录，如果登录成功调到首页，否则提示失败
   const res= await loginByPasswordAPI(mobile.value,password.value)
    store.setUser(res.data)
    showSuccessToast("登录成功")
   console.log(res);
  // 如果登录地址带有回跳地址的应该跳回那个地址去，没有的话跳回首页去
  router.push((route.query.returnUrl as string) || '/user')
  // Toast.success('登录成功')
}

// 发送验证码
// 有倒计时，如果倒计时没有归零验证码是不能被发送，
// 当倒计时归零后调用验证码发送接口进行发送
// 发送验证码后开启倒计时
// 当倒计时归零后停止计时
const time=ref(0)
const send=async()=>{
  if(time.value>0) return 
   
  // console.log("发送验证码")
const res= await sendCodeAPI(mobile.value,'login')
console.log(res);

  time.value=6
  let timerId
  // 开启定时器进行倒计时
   timerId=window.setInterval(()=>{
    time.value--
    if(time.value==0) window.clearInterval(timerId)
  },1000)

  
}


</script>



<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{isPassword? "密码登录":"短信验证码登录"}}</h3>
      <a href="javascript:;">
        <span @click="isPassword=!isPassword">{{isPassword? "短信验证码登录":"密码登录"}}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="login">
      <van-field 
      placeholder="请输入手机号"
      name="mobile"
      :rules="mobileRules" 
      v-model="mobile"
      type="tel"></van-field>
      <van-field v-if="isPassword"
      placeholder="请输入密码" 
      v-model="password"
      :rules="passwordRules" 
      :type="show?'text':'password'"
      > 
      <template #button>
        <cp-icon @click="show = !show" :name="`login-eye-${show ? 'on' : 'off'}`"></cp-icon>
      </template>
    </van-field>
      <van-field  v-else
       v-model="code"
       :rules="codeRules"
       placeholder="请输入验证码" 
       type="tel"
       >
      <template #button>
        <span class="btn-send" @click="send">{{ time>0? time+"s后发送":"验证码"}}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/logo.svg" alt="" />
      </div>
    </div>
  </div>

</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22,194,163,0.5);
    }
  }
}
</style>
